package com.zcy.pmp.controller;

import com.zcy.basic.annotation.PermissionApi;
import com.zcy.basic.util.AjaxResult;
import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.PageList;
import com.zcy.pmp.domain.Department;
import com.zcy.pmp.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author lucker
 * @date 2023/11/29 16:24
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门相关API", description = "部门的增删改查")
@PermissionApi(name = "部门管理", description = "部门相关的增删改查")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @ApiOperation("查询所有部门列表")
    @PermissionApi(name = "查询全部部门")
    public AjaxResult listAllDepartment() {
        try {
            List<Department> departments = departmentService.listAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping
    @ApiOperation("分页高级查询")
    @PermissionApi(name = "部门分页高级查询")
    public AjaxResult queryDepartmentPage(@RequestBody PageInfo pageInfo) {
        try {
            PageList<Department> departmentPageList = departmentService.queryPage(pageInfo);
            return AjaxResult.success(departmentPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PutMapping
    @ApiOperation("新增/修改部门")
    @PermissionApi(name = "新增/修改部门")
    public AjaxResult saveDepartment(@RequestBody Department department) {
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.add(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("通过ID删除部门")
    @PermissionApi(name = "通过ID删除部门")
    public AjaxResult deleteDepartmentById(@PathVariable("id") Long id) {
        try {
            departmentService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 批量删除
     * @param ids 部门ID数组
     * @return AjaxResult
     */
    @PatchMapping
    @ApiOperation("批量删除")
    @PermissionApi(name = "批量删除部门")
    public AjaxResult deleteBatchByDepartmentId(@ApiParam("部门ID数组") @RequestBody List<Long> ids) {
        try {
            departmentService.deleteBatch(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据Id查询部门")
    @PermissionApi(name = "根据Id查询部门")
    public AjaxResult queryDepartmentById(@PathVariable("id") Long id) {
        try {
            Department department = departmentService.queryById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    @GetMapping("/parents/{id}")
    @ApiOperation("根据id查询可用父部门树")
    @PermissionApi(name = "根据id查询可用父部门树")
    public AjaxResult queryDepartmentTree(@PathVariable("id") Long id) {
        try {
            List<Department> departments = departmentService.queryDepartmentTree(id);
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
